package com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.k_line_chart_portrait_content

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.JsonToken
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.LazyFragment
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.KlineChartInterface
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.bean.KlineChartTradeBean
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

//成交记录
class KlineChartPortraitContentTradeFragment : SimpleListFragment<KlineChartTradeBean>(),
    KlineChartPortraitLabel {

    companion object {
        fun newInstance(): KlineChartPortraitContentTradeFragment {
            val args = Bundle()
            val fragment = KlineChartPortraitContentTradeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var transactionUIInvokeListener: KlineChartInterface? = null

    private var timeHeaderTv:TextView?=null

    private var priceHeaderTv:TextView?=null

    private var numHeaderTv:TextView?=null

    private var onRefreshing = false

    override fun setTransactionUIInvokeListener(listener: KlineChartInterface?) {
        this.transactionUIInvokeListener = listener

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isAddDividerItemDecoration=false
        isEnableLoadMore=false
    }

    override fun getTitle(): String {
        return "成交记录"
    }

    @SuppressLint("SetTextI18n")
    override fun lazyInit() {
        val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
        val tokenName = transIndexNode?.iconType?.tokenName ?: ""
        val mTransferIcon = transIndexNode?.iconType?.getToTransferIcon()
        timeHeaderTv?.text="时间"
        priceHeaderTv?.text="价格($mTransferIcon)"
        numHeaderTv?.text="数量($tokenName)"
        netRequest()
    }

    override fun initView(view: View) {
        super.initView(view)
        mSwipeRefreshLayout.isEnabled = false
        mAdapter.removeAllHeaderView()
        val head=LayoutInflater.from(_mActivity)
            .inflate(R.layout.k_line_chart_portrait_content_trade_head_fragment,
                view as ViewGroup,false)

        mAdapter.addHeaderView(head)

        timeHeaderTv=head.findViewById(R.id.timeHeaderTv)
        priceHeaderTv=head.findViewById(R.id.priceHeaderTv)
        numHeaderTv=head.findViewById(R.id.numHeaderTv)
    }


    override fun getItemLayout(): Int {
        return R.layout.k_line_chart_portrait_content_trade_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: KlineChartTradeBean?) {
        val priceTvTextColor =
            if (item?.txType == 1) R.color.PrimaryColor else R.color.image_color_red
        helper?.setText(R.id.timeTv, item?.createTime)
            ?.setText(R.id.priceTv, "${item?.txPrice}")
            ?.setText(R.id.numTv, "${item?.txNums}")
            ?.setTextColorRes(R.id.priceTv,priceTvTextColor)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(KlineChartTradeBean::class.java)
            .endSubType()
            .build()
    }

    override fun netRequest() {

        if (onRefreshing && isSupportVisible) return
        onRefreshing = true

        val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
        val tokenName = transIndexNode?.iconType?.tokenName ?: ""
        val map = mapOf(
            "tokenType" to tokenName
        )
        RestClient.builder()
            .url(R.string.queryTxLog)
            .raw(JSON.toJSONString(map))
            .success {
                parseDate(it)
                onRefreshing=false
            }
            .failure {
                onFailed()
                onRefreshing=false
            }
            .error { _, _ ->
                onFailed()
                onRefreshing=false
            }
            .build()
            .post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["buyList"] ?: ""
        val array = JSON.parseArray(JSON.toJSONString(list), KlineChartTradeBean::class.java)
        loadListDate(array)
        mSwipeRefreshLayout.isEnabled = false
    }

    override fun onFailed() {
        super.onFailed()
        mSwipeRefreshLayout.isEnabled = false
    }

}